package com.xinjiang.electricpower.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinjiang.electricpower.domain.DTO.WorkOrderEarlyWarningDTO;
import com.xinjiang.electricpower.domain.DTO.WorkOrderPoolDTO;
import com.xinjiang.electricpower.domain.XjOrderGeneral;
import com.xinjiang.electricpower.domain.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface XjOrderGeneralMapper extends BaseMapper<XjOrderGeneral> {


    @Select(" select " +
            "xog.id," +
            "xog.sb_id," +
            "sd.dict_label as order_type_name," +
            "xog.order_no as generalOrderNo," +
            "xog.type as orderTypeCode," +
            "xos.system_name," +
            "so.org_name," +
            "xog.tg_name," +
            "xog.status," +
            "xog.activname," +
            "xoc.nature," +
            "xoc.priority," +
            "xog.dispatch_time," +
            "xog.usernames," +
            "xog.begin_date, " +
            "xog.finish_time, " +
            "concat(xog.longitude,',',xog.latitude) longtitudeAndLatitude " +
            "from xj_order_general xog " +
            "LEFT JOIN  sys_dict_data sd on xog.type = sd.dict_value and sd.dict_type = 'szh_order_general_type'" +
            "LEFT JOIN  xj_order_config xoc  on  xog.order_config_id = xoc.id  " +
            "LEFT JOIN  xj_order_source xos on xoc.source_id = xos.id " +
            "LEFT JOIN  sys_org so on xog.org_no = so.org_no " +
            " ${ew.customSqlSegment} ")
    List<WorkOrderPoolVO> findXjOrderPoolGenerals(@Param("ew") QueryWrapper<WorkOrderPoolVO> wrapper);

    /**
     * 工单预警按照各个类型汇总待办工单
     *
     * @return List<WorkOrderPoolSummaryVO> 返回汇总数据
     */
    @Select("select xog.type,count(xog.type) num from xj_order_general  xog        left join\n" +
            "    app_time_yj_xm atyx on xog.order_no = atyx.app_no " +
            " ${ew.customSqlSegment} ")
    List<WorkOrderPoolSummaryVO> selectXjOrderGeneralByStatus(@Param("ew") QueryWrapper<WorkOrderPoolSummaryVO> wrapper);

    @Select(" select distinct " +
            "xog.id," +
            "sd.dict_label as order_type_name," +
            "xog.order_no," +
            "xos.system_name," +
            "so.org_name," +
            "xog.tg_name," +
            "xog.type as orderTypeCode," +
            "xoc.priority," +
            "xog.status," +
            "xog.activname," +
            "xog.dispatch_time," +
            "xog.usernames," +
            "xog.begin_date, " +
            "xog.finish_time " +
            "from xj_order_general xog " +
            "LEFT JOIN  sys_dict_data sd on xog.type = sd.dict_value and sd.dict_type = 'szh_order_general_type'" +
            "LEFT JOIN  xj_order_config  xoc on  xog.order_config_id = xoc.id  " +
            "LEFT JOIN  xj_order_source xos on xoc.source_id = xos.id " +
            "LEFT JOIN  sys_org so on xog.org_no = so.org_no " +
            " ${ew.customSqlSegment} ")
    List<XjOrderGeneralVO> findXjOrderGenerals(@Param("ew") QueryWrapper<XjOrderGeneralVO> wrapper);

    @Select(" select " +
            "xog.id," +
            "xog.type as orderTypeCode," +
            "sd.dict_label as order_type_name," +
            "xog.general_order_no  order_no," +
            "xog.order_no   general_order_no," +
            "xos.system_name," +
            "so.org_name," +
            "xog.tg_name," +
            "xog.status," +
            "xog.activname," +
            "xog.dispatch_time," +
            "xog.usernames," +
            "xog.begin_date create_time " +
            "from xj_order_general xog " +
            "LEFT JOIN  sys_dict_data sd on xog.type = sd.dict_value and sd.dict_type = 'szh_order_general_type'" +
            "LEFT JOIN xj_order_config xoc on  xog.order_config_id = xoc.id  " +
            "LEFT JOIN  xj_order_source xos on xoc.source_id = xos.id " +
            "LEFT JOIN  sys_org so on xog.org_no = so.org_no " +
            " ${ew.customSqlSegment} ")
    List<WorkOrderMonitorVO> findWorkOrderMonitors(@Param("ew") QueryWrapper<WorkOrderMonitorVO> wrapper);

    @Select("select xos.id," +
            "count(xog.id) order_num, " +
            "xos.system_name," +
            "null list " +
            "from xj_order_general xog " +
            "INNER JOIN xj_order_config xoc on xog.order_config_id = xoc.id\n" +
            "INNER JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            " ${ew.customSqlSegment} ")
    List<SystemOrdersVO> countSystemOrders(@Param("ew") QueryWrapper<SystemOrdersVO> wrapper);


    @Select("select xos.id," +
            "count(xog.id) order_num, " +
            "xos.system_name," +
            "null list " +
            "from xj_order_general xog " +
            "LEFT JOIN xj_order_config xoc on xog.order_config_id = xoc.id\n" +
            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            " ${ew.customSqlSegment} ")
    List<SystemOrdersVO> countSystemOrder(@Param("ew") QueryWrapper<SystemOrdersVO> wrapper);

    @Select("select\n" +
            " xog.type,\n" +
            " count(xog.type) num\n" +
            " from xj_order_general xog\n" +
            " Left join xj_order_config xoc  on xog.order_config_id = xoc.id\n" +
            " Left join xj_order_source xos on xoc.source_id = xos.id" +
            " ${ew.customSqlSegment} ")
    List<OrderTypeStatisticsVO> countOrderTypeBySystem(@Param("ew") QueryWrapper<XjOrderGeneralVO> wrapper);

    @Select(" select xos.id,count(xog.id) order_num, " +
            "xos.id," +
            "xos.system_name " +
            "from xj_order_general xog " +
            "INNER JOIN xj_order_config xoc  on xog.order_config_id = xoc.id\n" +
            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            " ${ew.customSqlSegment} ")
    List<SystemOrdersVO> countSystemOrderCondition(@Param("ew") QueryWrapper<SystemOrdersVO> queryWrapper);


    @Select(" select " +
            "xog.tg_id, " +
            "xog.tg_name, " +
            "count(xog.id) nums " +
            "from xj_order_general xog " +
            "LEFT JOIN xj_order_config xoc on xog.order_config_id = xoc.id\n" +
            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            "${ew.customSqlSegment} ")
    List<TgWorkOrderStatisticVO> countTgOrders(@Param("ew") QueryWrapper<TgWorkOrderStatisticVO> queryWrapper);

    @Select(" select " +
            "count(atyx.id) nums, " +
            "atyx.app_yjdj as priority\n" +
//            "from xj_order_general xog " +
//            "LEFT JOIN xj_order_config xoc on xog.order_config_id = xoc.id\n" +
//            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
//            "left join app_time_yj_xm atyx on atyx.app_no = xog.order_no\n" +
            "from app_time_yj_xm atyx \n" +
            "${ew.customSqlSegment} ")
    List<WarningUrgentVO> countPriorityOrders(@Param("ew") QueryWrapper<EventProcessingVO> queryWrapper);

    @Select(" select xog.status , " +
            "count(xog.status) nums " +
            "from xj_order_general xog " +
            "LEFT JOIN xj_order_config xoc on xog.order_config_id = xoc.id\n" +
            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            "${ew.customSqlSegment} ")
    List<WorkOrderStatisticsStatusVO> countWorkOrdersByStatus(@Param("ew") QueryWrapper<XjOrderGeneral> queryWrapper);

    @Select(" select count(xog.id) order_num, " +
            "xos.system_name, " +
            "xos.id source_id  " +
            " from xj_order_source  xos \n" +
            " Left Join xj_order_config xoc on xoc.source_id = xos.id \n" +
            " Left join xj_order_general xog on xog.order_config_id = xoc.id and DATE_FORMAT(xog.dispatch_time,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d') \n" +
            "${ew.customSqlSegment}"
            )
    List<SystemWorkOrderStatisticsStatusVO> countSystemWorkProcessOrders(@Param("ew") QueryWrapper<SystemWorkOrderStatisticsStatusVO> queryWrapper);

    @Select(" select count(0) nums, " +
            "xog.business_type as nature, " +
            "xog.type as type " +
            "from xj_order_general xog " +
            "LEFT JOIN xj_order_config xoc  on xog.order_config_id = xoc.id\n" +
            "LEFT JOIN xj_order_source xos on xoc.source_id = xos.id\n" +
            "${ew.customSqlSegment}")
    List<WorkOrderNatureStatisticVO> countNatureOrders(@Param("ew") QueryWrapper<WorkOrderNatureStatisticVO> queryWrapper);

//    @Select("select xoc.priority , atyx.processing_status ,count(atyx.processing_status) num\n" +
//            "from xj_order_general xog\n" +
//            "         LEFT JOIN xj_order_config xoc  on xog.order_config_id = xoc.id\n" +
//            "         LEFT JOIN app_time_yj_xm atyx on xog.order_no = atyx.app_no\n" +
//            " ${ew.customSqlSegment} ")
    @Select("select atyx.app_yjdj as priority, atyx.processing_status ,count(atyx.processing_status) num\n" +
            "from app_time_yj_xm atyx\n" +
            " ${ew.customSqlSegment} ")
    List<EventProcessingVO> selectEventProcessingVO(@Param("ew") QueryWrapper<WorkOrderEarlyWarningDTO> queryWrapper);

    @Select(" select  " +
            "atyx.id," +
            "atyx.app_yjmc," +
            "atyx.app_no ," +
            //异常类型/工单/数据
            "atyx.app_yjxl," +
            "atyx.yj_content," +
            "atyx.app_yjdm," +
            "atyx.app_type_code, " +
            //"xoc.priority," +
            "atyx.tg_no," +
            "atyx.tg_name," +
            "atyx.cons_no," +
            "atyx.cons_name," +
            //"xog.content," +
            "atyx.assigner," +
            "atyx.assigner_name, " +
            "atyx.tj_time, " +
            //"xog.status, " +
            "atyx.processing_status, " +
            "atyx.handle_time, " +
            "atyx.app_yjdj as priority " +
            "from app_time_yj_xm atyx " +
            //"left join xj_order_general xog on  xog.order_no = atyx.app_no  " +
            //"left join  xj_order_config xoc on xoc.id= xog.order_config_id " +
            " ${ew.customSqlSegment} ")
    List<MyEarlyWarningVO> findMyEarlyWarnings(@Param("ew") QueryWrapper<MyEarlyWarningVO> wrapper);

    @Select(" select " +
            "atyx.id," +
            "atyx.app_yjmc," +
            "atyx.app_no," +
            //异常类型/工单/数据
            "atyx.app_yjxl," +
            "atyx.app_yjdm," +
            "atyx.yj_content," +
            "atyx.app_type_code," +
            //"xoc.priority," +
            "atyx.tg_no," +
            "atyx.tg_name," +
            "atyx.cons_no," +
            "atyx.cons_name," +
            //"xog.content," +
            "atyx.assigner," +
            "atyx.assigner_name," +
            "atyx.tj_time, " +
            "atyx.handle_time, " +
            //"xog.status, " +
            "atyx.app_yjdj as priority, " +
            "atyx.processing_status " +
            "from app_time_yj_xm atyx " +
//            "inner join xj_order_general xog on  xog.order_no = atyx.app_no  " +
//            "inner join  xj_order_config xoc on xoc.id= xog.order_config_id " +
            " ${ew.customSqlSegment} ")
    MyEarlyWarningVO findMyEarlyWarning(@Param("ew") QueryWrapper<MyEarlyWarningVO> wrapper);

    @Select("select id , yj_content from app_time_yj_content ${ew.customSqlSegment}")
    WarningContentVO selectWarningContent(@Param("ew") QueryWrapper<MyEarlyWarningVO> wrapper);

    @Update("update app_time_yj_xm set processing_status = #{processingStatus}  where id = #{id} ")
    int updateProcessStatus(@Param(value = "id") String id, @Param(value = "processingStatus") int processingStatus);

    /**
     * 工单池按照台区工单类型统计
     * @param workOrderPoolDTO
     * @param authStr
     * @return
     */
    List<TgWorkOrderStatisticVO> selectXjOrderGeneralsTest(@Param("dto")WorkOrderPoolDTO workOrderPoolDTO, @Param("auth")AuthInfoVo authStr);

    /**
     * 工单业务占比
     * @param workOrderPoolDTO
     * @param authStr
     * @return
     */
    List<SystemOrdersVO> countSystemOrderTest(@Param("dto")WorkOrderPoolDTO workOrderPoolDTO, @Param("auth")AuthInfoVo authStr);

    /**
     * 工单类型占比子列表
     * @param workOrderPoolDTO
     * @param authStr
     * @return
     */
    List<OrderTypeStatisticsVO> countOrderTypeBySystemTest(@Param("dto")WorkOrderPoolDTO workOrderPoolDTO, @Param("auth")AuthInfoVo authStr ,@Param("sourceId") String id);

    /**
     * 工单池列表
     * @param workOrderPoolDTO
     * @return
     */
    List<WorkOrderPoolVO> findXjOrderPoolGeneralsTest(@Param("dto") WorkOrderPoolDTO workOrderPoolDTO);
}
